<style scoped>
</style>

<template>

  <div>

    <Modal
      v-model="IsModalShow"
      :title="modalFormTitle"
      :mask-closable="false"
      @on-cancel="cancel"
      width="600"
    >
      <Form
        ref="modalForm"
        :model="modalForm"
        :label-width="80"
        value=true
        style="padding: 3px 60px"
      >
        <Form-item
          label="部门名"
          prop="name"
          :rules="{required: true, message: '必填,1-10位字符',min:1,max:10, trigger:'blur',type:'string'}"
        >
          <Input v-model="modalForm.name"></Input>
        </Form-item>
        <Form-item
          label="上级部门"
          v-if="modalForm.parentId!==0"
        >
          <Cascader
            v-model="modalForm.cascader"
            :data="cascaderData"
            change-on-select
          ></Cascader>
        </Form-item>
      </Form>
      <div slot="footer">
        <Button
          type="default"
          @click="cancel"
        >关闭</Button>
        <Button
          type="primary"
          :loading="modalForm_loading"
          @click="saveForm('modalForm')"
        >保存</Button>
      </div>
    </Modal>
  </div>

</template>

<script>
import { saveDept, updateDept, getDeptCascader } from "@/api/system";
export default {
  props: {
    parentForm: {
      type: Object,
      default: function() {
        return {
          deptId: "",
          name: "",
          orderNum: "",
          parentId: "",
          parentName: "",
          cascader: ""
        };
      }
    },
    modalShow: {
      type: Boolean,
      default: true
    },
    modalFormTitle: {
      type: String,
      default: "添加部门"
    }
  },
  data() {
    return {
      IsModalShow: false,
      modalForm: {},
      modalForm_loading: false,
      cascaderData: []
    };
  },
  watch: {
    modalShow(curVal, oldVal) {
      if (curVal) {
        this.getDeptCascader();
        let parentFormVal = JSON.parse(JSON.stringify(this.parentForm));
        if (parentFormVal.cascader) {
          parentFormVal.cascader = parentFormVal.cascader
            .split(",")
            .map(val => parseInt(val));
        } else {
          parentFormVal.cascader = [];
        }
        this.modalForm = parentFormVal;
      }

      this.IsModalShow = curVal;
    }
  },
  created() {},
  mounted() {},
  methods: {
    cancel() {
      this.$emit("listenModalForm");
    },
    async getDeptCascader() {
      const result = await getDeptCascader();
      if (result.code == 0) {
        this.cascaderData = result.data;
      }
    },
    saveForm(name) {
      this.$refs[name].validate(async valid => {
        if (valid) {
          this.modalForm_loading = true;
          let cascader = this.modalForm.cascader.join(",");
          let parentId = this.modalForm.cascader[
            this.modalForm.cascader.length - 1
          ];
          let params = JSON.parse(JSON.stringify(this.modalForm));
          params.cascader = cascader;
          params.parentId = parentId;
          try {
            let result;
            if (this.modalFormTitle == "添加部门") {
              result = await saveDept(params);
            } else {
              result = await updateDept(params);
            }

            if (result.code == 0) {
              this.$emit("listenModalForm");
              this.$emit("refreshTableList");
            }
          } catch (err) {
            console.log(err);
            this.$Message.error("服务器异常，稍后再试");
          }
          this.modalForm_loading = false;
        } else {
          this.$Message.error("表单验证失败!");
        }
      });
    }
  }
};
</script>
